草庐IT

java 中构造器: this的两种用法

全部标签

javascript - 调用 $(this) 时,jQuery 会重新查询 DOM 吗?

在下面的代码中,当$(this)被调用时,jQuery是否重新查询DOM,就好像选择器已传递给它一样(使用对象的某些属性作为选择器),或者jQuery是否保留先前返回的对象?$('.someButton').on('click',function(){$(this).remove();//Isthisanotherlookup,orjustawrapperforthepreviouslyreturnedobject?}); 最佳答案 它不会重新查询DOM,this已经是一个元素。jQuery只是将上下文设置为元素,调整长度,然后返回

javascript - 构造函数中的 "use strict"是否扩展到原型(prototype)方法?

我试图弄清楚“usestrict”的定义是否扩展到构造函数的原型(prototype)方法。示例:varMyNamespace=MyNamespace||{};MyNamespace.Page=function(){"usestrict";};MyNamespace.Page.prototype={fetch:function(){//doIneedtouse"usestrict"hereagain?}};根据Mozilla您可以将其用作:functionstrict(){"usestrict";functionnested(){return"AndsoamI!";}return"Hi

javascript - JQuery $(this) 在函数参数中不起作用

以下代码无效:$(".countdown").circularCountdown({startDate:$(this).attr('data-start'),endDate:$(this).attr('data-end'),timeZone:$(this).attr("timezone")});下面那个工作正常,$(".countdown").circularCountdown({startDate:$(".countdown").attr('data-start'),endDate:$(".countdown").attr('data-end'),timeZone:$(".count

javascript - "this"上下文输出无法理解

我很难理解下面的代码。functionfoo(){console.log(this.a);}varobj={a:2,foo:foo};vara=4;obj.foo();setTimeout(obj.foo,100);setTimeout(obj.foo.bind(obj),100);它的输出为2、4、2,我无法理解。 最佳答案 第一种情况,obj.foo();foo中的this将指向obj,因为您已将该函数分配为该特定对象的属性。第二种情况,setTimeout(obj.foo,100);在setTimeout中,传递的函数将在窗口

javascript - 用于设置实例属性的 ES6 类构造函数快捷方式

我似乎记得看到一个快捷方式,如果属性和构造函数参数被命名为相同的东西,你不必在构造函数中执行this.foo赋值-但我似乎无法找到它的引用谷歌搜索。例如:classPolygon{constructor(height,width){this.height=height;this.width=width;}}你能不能做一些类似的事情classPolygon{constructor(height=height,width=width){//wasn'tthereawaytodeclaretheseargumentssoitautosetstheinstancevariables?}}

Javascript 将可变参数传递给父类(super class)构造函数

将可变参数传递给父类(superclass)构造函数的最佳/推荐方法是什么?背景解释了我试图解决的问题。背景我正在将一些代码从Java移植到Javascript。Java的编码模式之一是函数重载。Java选择最佳匹配来确定要调用的函数。当函数是类构造函数时,这会变得很有趣。所以Java中的代码可能是publicclassMyParserextendsParser{publicintparse(Stringstr){super(str);...}publicintparse(Stringstr,intbase){super(str,base);...}}在Javascript中变成:cl

javascript - 如何在不使用 JavaScript ES6 中的构造函数的情况下使用对象文字来创建类的实例?

我正在尝试学习JavaScriptES6,这是一种非常酷的语言,我认为我应该练习一下,但我做不到anexercise.那么如何使用对象字面量来复制一个类。例如类是:classPoint{constructor(x,y){this.x=x,this.y=y}add(other){returnnewPoint(this.x+other.x,this.y+other.y)}}我想在这里使用对象字面量来使输出为真。varfakePoint=YOUR_CODE_HEREconsole.log(fakePointinstanceofPoint) 最佳答案

javascript - 混合构造函数并在 Javascript 代理对象上应用陷阱

我有一个类,我想对其应用代理,观察方法调用和构造函数调用:计算器.jsclassCalc{constructor(){}add(a,b){returna+b;}minus(a,b){returna-b;}}module.exports=Calc;index.jsconstCalculator=require('./src/Calculator');constCalculatorLogger={construct:function(target,args,newTarget){console.log('Objectinstantiated');returnnewtarget(...arg

javascript - 防止 this.state 与 setState 一起使用

Thereference状态:setState()doesnotalwaysimmediatelyupdatethecomponent.Itmaybatchordefertheupdateuntillater.Thismakesreadingthis.staterightaftercallingsetState()apotentialpitfall.Instead,usecomponentDidUpdateorasetStatecallback(setState(updater,callback)),eitherofwhichareguaranteedtofireaftertheupd

javascript - 停止在临时变量中保存 'this'

我一直不得不将this保存在一个临时变量中,以便在其他函数中访问它。例如,在下面的两个方法中,我将this保存在that变量中:startTimer:function(){varthat=this;if($('#defaultCountdown:hidden'))$('#defaultCountdown').show('slow');shortly=newDate();shortly.setSeconds(shortly.getSeconds()+5);$('#defaultCountdown').countdown('change',{until:shortly,layout:'Ne